<?php
	require '../connect/connect.php';
	include '../template/template_main.tpl';
	require 'epocaCtrl.php';
	
	if(isset($_SESSION["profile"]) && !($_SESSION["profile"] == "2"))
		header("Location: ../index.php");
	if (isset($_POST["nomeEpoca"]))
	{
		$nomeEpoca = trim($_POST["nomeEpoca"]);
		$dataInicio = trim($_POST["dataInicio"]);
		$dataFim = trim($_POST["dataFim"]);
		
		$error = array();
		
		if ($nomeEpoca == "")
			$error[] = "Deve preencher o nome da epoca.";
		
		if ($dataInicio == "")
			$error[] = "Deve preencher a data de inicio.";
			
		if ($dataInicio == "")
			$error[] = "Deve preencher a data final.";
			
		if (count($error) == 0)
		{
			if (preg_match ("/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/", $dataInicio, $parts))
			{
				if(!checkdate($parts[2],$parts[1],$parts[3]))
					$error[] = "Data Inicio: Data está incorrecta.";
			}
			else
				$error[] = "Data Inicio: Formato da data 'DD-MM-YYYY'";
			
			if (preg_match ("/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/", $dataFim, $parts))
			{
				if(!checkdate($parts[2],$parts[1],$parts[3]))
					$error[] = "Data Final: Data está incorrecta.";
			}
			else
				$error[] = "Data Final: Formato da data 'DD-MM-YYYY'";
			
			if (count($error) == 0)
			{
				$dtDataInicio = new DateTime($dataInicio." 00:00:00");
				$dtDataFim = new DateTime($dataFim." 00:00:00");
				
				if ($dtDataInicio >= $dtDataFim)
					$error[] = "A data de inicio deve ser superior à data final.";
				else 
				{
					$dataInicio = $dtDataInicio->format("Y-m-d");
					$dataFim = $dtDataFim->format("Y-m-d");
					
					$error = adicionarEpoca($conn, $nomeEpoca ,$dataInicio, $dataFim);
					
					if (count($error) == 0)
					{
						$mensagem = "Epoca adicionada com sucesso";
						$_POST["nomeEpoca"] = "";
						$_POST["dataInicio"] = "";
						$_POST["dataFim"] = "";
					}
				}
			}
		}
	}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="../css/template.css" />
<link rel="stylesheet" type="text/css" href="../css/corpo_main.css" />
<link rel="stylesheet" type="text/css" href="../css/form.css" />
<script type="text/javascript" src="../js/jquery-1.7.2.js" ></script>
<script type="text/javascript" src="../js/main.js" ></script>

<title>.:MateMatrix:.</title>
</head>
<body>
	<?php makeHeader(); ?>
	<?php echo $estruturaInicio;?>
	<h1>- Nova Epoca -</h1>
	<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
		<div id="teamTable">
		<?php
			if (isset($error) && (count($error) > 0))
			{
				echo "<div class=\"erros\"><ul>";
				foreach ($error as $value) {
					printf("<li>%s</li>", $value);
				}
				echo "</ul></div>";
			}
			if (isset($mensagem))
			{
				echo "<div class=\"info\"><ul><li>$mensagem</li></ul></div>";
			}
		?>
		<br />
			<div class="ladoEsq">
				<label>Nome da Epoca</label>
			</div>
			<div class="ladoDir">	
				<input type="text" class="textbox" name="nomeEpoca" value="<?php if (isset($_POST["nomeEpoca"])) echo htmlspecialchars($_POST["nomeEpoca"]); ?>"/>
			</div>
			<div class="ladoEsq">
				<label>Data Inicio</label>
			</div>
			<div class="ladoDir">
				<input type="text" class="textbox" name="dataInicio" value="<?php if(isset($_POST["dataInicio"])) echo htmlspecialchars($_POST["dataInicio"]); ?>"/>
			</div>
			<div class="ladoEsq">
				<label>Data Final</label>
			</div>
			<div class="ladoDir">
				<input type="text" class="textbox" name="dataFim" value="<?php if(isset($_POST["dataFim"])) echo htmlspecialchars($_POST["dataFim"]);  ?>"/>
			</div>
		</div>
		<div class="ladoEsq">
			<input id="submit" class="textbox" type="submit" value="Guardar"/>
		</div>
		<div class="ladoDir">
			<input class="textbox" type="reset" value="Limpar"/>
		</div>
	</form>

	<?php echo $estruturaFim; ?>
	<?php echo $footer;?>
</body>
</html>